package com.liu.thread.ch08;

public class T8 implements Runnable {

	
	private  int ticket=20;
	private Boolean flag=true;
	
	@Override
	public void run() {
		
		while(true){
			if(flag){
				flag=sellTicket();
			}else{
				break;
			}
		}
		
	}
	
//	synchronized表示同一个时间能有一个线程来执行此方法
	@SuppressWarnings("static-access")
	public synchronized boolean sellTicket(){
		
		if(ticket>0){
			try {
				Thread.currentThread().sleep(300);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println(Thread.currentThread().getName()+"售票，票号"+ticket);
			ticket=ticket-1;
			return true;
		}else{
			return false;
		}
		
	}
	
	

}
